<template>
  <div>
    <el-card class="box-card">
      <el-form ref="queryFormRef" :model="queryInfo" size="small" :inline="true">
        <el-form-item label="姓名" prop="empName">
          <el-input
            v-model="queryInfo.empName"
            @keyup.enter.native="handleQuery"
            clearable
            placeholder="输入姓名"
          ></el-input>
        </el-form-item>
        <el-form-item label="身份证号码" prop="idCard">
          <el-input
            v-model="queryInfo.idCard"
            @keyup.enter.native="handleQuery"
            clearable
            placeholder="输入身份证号码"
          ></el-input>
        </el-form-item>
        <el-form-item label="选择部门" prop="deptId">
          <dept-select :value.sync="queryInfo.deptId" style="width:220px" />
        </el-form-item>

        <el-form-item label="选择岗位" prop="staId">
          <station-select :value.sync="queryInfo.staId" />
        </el-form-item>

        <el-form-item label="用工形式" prop="empType">
          <el-select v-model="queryInfo.empType" placeholder="选择用工形式">
            <el-option label="正式员工" :value="true"></el-option>
            <el-option label="临时员工" :value="false"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button size="mini" type="primary" @click="handleQuery">查询</el-button>
          <el-button size="mini" @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table
        ref="multipleTable"
        :data="employeeInfoList"
        tooltip-effect="dark"
        style="width: 100%;"
        v-loading="loading"
        :header-cell-style="{ background: '#f8f8f9', color: '#515a6e' }"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" />
        <el-table-column prop="empName" label="真实姓名" />
        <el-table-column label="性别">
          <template slot-scope="scope">
            <span>{{ scope.row.sex === 1 ?'男': '女' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="idCard" label="身份证号码" width="180" align="center" />

        <el-table-column prop="empPhone" label="手机号码" width="120px" />
        <el-table-column prop="department.deptName" label="部门名称" />
        <el-table-column prop="station.staName" label="岗位名称" />

        <el-table-column label="用工形式">
          <template slot-scope="scope">
            <span>{{ scope.row.empType ? '正式员工': '临时员工' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="入职时间" width="140px">
          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            <span style="margin-left: 10px">{{ scope.row.empDate | formatDate }}</span>
          </template>
        </el-table-column>
        <el-table-column label="人员来源">
          <template slot-scope="scope">
            <span>{{ ['校园招聘','社会招聘','其他'][scope.row.source - 1] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="handleDeparture(scope.row.id)"
              icon="iconfont iconrenyuanlikai"
            >离职</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="getEmployeeInfoList"
        @current-change="getEmployeeInfoList"
        :current-page.sync="queryInfo.pageNum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size.sync="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <el-dialog title="申请离职" :visible.sync="dialogVisible" width="500px" @close="handleClose">
      <el-form ref="departureFormRef" :model="departureForm" label-width="110px" :rules="rules">
        <el-form-item label="离职类型" prop="disType">
          <el-select v-model="departureForm.disType" placeholder="请选择活动区域" style="width:100%;">
            <el-option label="主动辞职" :value="1"></el-option>
            <el-option label="辞退" :value="2"></el-option>
            <el-option label="退休" :value="3"></el-option>
            <el-option label="开除" :value="4"></el-option>
            <el-option label="试用不通过" :value="5"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="离职日期" prop="disDate">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="departureForm.disDate"
            value-format="yyyy-MM-dd"
            style="width: 100%;"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="去向" prop="direction">
          <el-input v-model="departureForm.direction"></el-input>
        </el-form-item>
        <el-form-item label="是否进人才库" prop="isToTalent">
          <el-radio-group v-model="departureForm.isToTalent">
            <el-radio :label="true">是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="disRemark">
          <el-input type="textarea" v-model="departureForm.disRemark"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm('departureFormRef')">重置</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listEmployeeInfo } from "@/api/employee";
import { submitDeparture } from "@/api/dismission";

export default {
  name: "Departure",
  data() {
    return {
      queryInfo: {
        // 当前页
        pageNum: 1,
        // 页大小
        pageSize: 10,
        empName: undefined,
        deptId: undefined,
        staId: undefined,
        empType: undefined,
        idCard: undefined,
      },
      departureForm: {
        empId: undefined,
        disType: 1,
        disDate: undefined,
        direction: undefined,
        isToTalent: true,
        disRemark: undefined,
      },
      employInfoFormDisable: true,
      employeeInfoList: [],
      total: 0,
      loading: false,
      dialogVisible: false,
      rules: {
        disType: [
          { required: true, message: "请选择离职类型", trigger: "blur" },
        ],
        disDate: [
          { required: true, message: "请选择离职日期", trigger: "blur" },
        ],
        isToTalent: [
          {
            required: true,
            message: "请选择是否进人才库",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getEmployeeInfoList();
  },
  computed: {
    token() {
      return this.$store.state.userModule.token;
    },
  },
  methods: {
    async getEmployeeInfoList() {
      this.loading = true;
      let resp = await listEmployeeInfo(this.queryInfo);
      if (resp.code === 200) {
        let { data } = resp;
        console.log(data);
        this.total = data.total;
        this.employeeInfoList = data.employees;
      }
      this.loading = false;
    },
    handleSelectionChange() {},
    async handleDeparture(id) {
      this.dialogVisible = true;
      this.departureForm.empId = id;
    },
    // 关键字查询
    handleQuery() {
      this.queryInfo.pageNum = 1;
      console.log(this.queryInfo);
      this.getEmployeeInfoList();
    },
    // 重置表单
    handleReset() {
      this.$refs.queryFormRef.resetFields();
      console.log(this.queryInfo);
      this.getEmployeeInfoList();
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleSubmit() {
      //   console.log(this.departureForm);
      this.$refs.departureFormRef.validate(async (valid) => {
        if (!valid) return;
        const { code } = await submitDeparture(this.departureForm);
        if (code === 200) {
          this.dialogVisible = false;
          this.$message("该员工已离职！");
          this.getEmployeeInfoList();
        }
      });
    },
    handleClose() {
      this.resetForm("departureFormRef");
    },
  },
};
</script>

<style scoped lang="scss">
.el-pagination {
  margin: 18px 0px;
  float: right;
}
/deep/ .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
/deep/ .none-border .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 130px;
  height: 148px;
  line-height: 148px;
  text-align: center;
}
.avatar {
  width: 130px;
  height: 148px;
  object-fit: cover;
  display: block;
}
.phone {
  width: 100%;
  display: flex;
}
</style>
<style lang="scss">
.phoen {
  .el-form-item_content {
    flex: 1 !important;
  }
}
</style>